package com.java110.mall.acct.payment.business;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.payment.PaymentOrderDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.storeOrder.StoreOrderCartFeeDto;
import com.java110.intf.cart.IStoreOrderCartFeeInnerServiceSMO;
import com.java110.intf.cart.IUnifiedOrderInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.mall.acct.payment.IPaymentBusiness;
import com.java110.utils.cache.CommonCache;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service("buyServUnPayFee")
public class BuyServUnPayFeeBusiness implements IPaymentBusiness {

    private static final Logger logger = LoggerFactory.getLogger(BuyServUnPayFeeBusiness.class);

    public static final String PAY_SERV_FEE = "PAY_SERV_FEE_";

    @Autowired
    private IStoreOrderCartFeeInnerServiceSMO storeOrderCartFeeInnerServiceSMOImpl;

    @Autowired
    private IUnifiedOrderInnerServiceSMO unifiedOrderInnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Override
    public PaymentOrderDto unified(ICmdDataFlowContext context, JSONObject reqJson) {
        String appId = context.getReqHeaders().get("app-id");
        String userId = context.getReqHeaders().get("user-id");
        reqJson.put("user-id", userId);//
        reqJson.put("appId", appId);//
        //todo 查询商铺信息

        StoreOrderCartFeeDto storeOrderCartFeeDto = new StoreOrderCartFeeDto();
        storeOrderCartFeeDto.setShopId(reqJson.getString("shopId"));
        storeOrderCartFeeDto.setCartId(reqJson.getString("cartId"));
        storeOrderCartFeeDto.setState(StoreOrderCartFeeDto.STATE_W);

        List<StoreOrderCartFeeDto> storeOrderCartFeeDtos = storeOrderCartFeeInnerServiceSMOImpl.queryStoreOrderCartFees(storeOrderCartFeeDto);

        if (ListUtil.isNull(storeOrderCartFeeDtos)) {
            throw new CmdException("未包含费用");
        }

        String extOrderId = GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_orderId);
        double money = 0.0;
        BigDecimal tmpMoney = new BigDecimal(money);
        BigDecimal feePrice = null;
        String orderId = "";
        String cartId = "";
        for (StoreOrderCartFeeDto tmpStoreOrderCartFeeDto : storeOrderCartFeeDtos) {
            feePrice = new BigDecimal(tmpStoreOrderCartFeeDto.getPayPrice());
            tmpMoney = tmpMoney.add(feePrice);
            cartId = tmpStoreOrderCartFeeDto.getCartId();
            orderId = tmpStoreOrderCartFeeDto.getOrderId();
        }
        money = tmpMoney.setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();

        //计算服务费
        ShopDto shopDto = new ShopDto();
        shopDto.setShopId(reqJson.getString("shopId"));
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);

        Assert.listOnlyOne(shopDtos, "商铺不存在");
        shopDto = shopDtos.get(0);

        String feeName = "家政服务";

        PaymentOrderDto paymentOrderDto = new PaymentOrderDto();
        paymentOrderDto.setOrderId(extOrderId);
        paymentOrderDto.setMoney(money);
        paymentOrderDto.setName(feeName);
        if (StringUtil.isNumber(shopDto.getServiceFee()) && Double.parseDouble(shopDto.getServiceFee()) > 0) {
            paymentOrderDto.setProfitSharing(true);
        }
        JSONObject saveFees = new JSONObject();
        saveFees.put("cartId", cartId);
        saveFees.put("orderId", orderId);
        saveFees.put("money", money);
        saveFees.put("poolId", storeOrderCartFeeDtos.get(0).getPoolId());
        saveFees.put("shopId", storeOrderCartFeeDtos.get(0).getShopId());
        saveFees.put("fees", storeOrderCartFeeDtos);
        CommonCache.setValue(PAY_SERV_FEE + extOrderId, saveFees.toJSONString(), CommonCache.PAY_DEFAULT_EXPIRE_TIME);
        return paymentOrderDto;
    }

    @Override
    public void notifyPayment(PaymentOrderDto paymentOrderDto, JSONObject reqJson) {

        String order = CommonCache.getAndRemoveValue(PAY_SERV_FEE +  paymentOrderDto.getOrderId());
        if (StringUtil.isEmpty(order)) {
            return ;// 说明已经处理过了 再不处理
        }
        JSONObject orderInfo = JSONObject.parseObject(order);

        logger.debug("订单信息"+order);


        reqJson.put("orderId", orderInfo.getString("orderId"));
        reqJson.put("payOrderId", paymentOrderDto.getTransactionId());
        reqJson.put("extOrderId", paymentOrderDto.getOrderId());

        ResponseEntity<String> responseEntity = unifiedOrderInnerServiceSMOImpl.payNotifyOrder(reqJson);
    }
}
